default_platform(:ios)

# 网络请求依赖
require 'net/http'
require 'uri'
require 'json'

platform :ios do
  
  # release发布包
  desc "Push a new release build to the App Store"
  lane :release do
    increment_build_number(xcodeproj: "DaRenShop.xcodeproj")
    build_app(workspace: "DaRenShop.xcworkspace", scheme: "DaRenShop")
    upload_to_app_store
  end

  # debug测试包
  desc "Push a new debug build to the pgyer.com"
  # ipa_dir  = "./fastlane/build/"
  ipa_dir  = "/Users/gaobing/Desktop/ipa"

  ipa_name = "DianBo" + Time.new.strftime('%Y-%m-%d_%H:%M')

  lane :debug do |options|
  gym(
    # 打包前clean项目
    clean: true, 
    # 构建时，隐藏不必要的信息  
    silent: true,           
    # bitcode
    include_bitcode: false,                             
    # 导出方式 app-store、ad-hoc、enterprise、development
    export_method: "development",        
    # scheme    
    # scheme: "DianBoTest",         
   scheme: "DianBoFastlane",                 
        
    # 环境 Debug、Release
    configuration: "Debug",             
    # ipa的存放目录
    output_directory: ipa_dir,          
    # 输出ipa的文件名为当前的build号
    output_name: ipa_name                 
    )
  # 蒲公英
  pgyer(
    #build_app(export_method: "ad-hoc"),
    api_key: "9be763cf7766a51db7cd7fa0b571e5dd", 
    user_key: "2d2bf6fb3df3a5c17c66f155c0feb8df",
    #设置一个版本更新时的描述信息
    update_description: options[:desc]
   )

  #上传ipa到fir.im服务器，在fir.im获取firim_api_token
  #firim(firim_api_token: "bfdd79fa24b31633f3ac75d68e7a8110")

# 钉钉机器人
  app_patch   = ipa_dir + "/#{ipa_name}.ipa"
  app_version = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleShortVersionString")
  app_build_version = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleVersion")
  app_name    = get_ipa_info_plist_value(ipa: app_patch, key: "CFBundleDisplayName")

  app_Environmental = ""
  app_change_content = ""

 dingTalk_url = "https://oapi.dingtalk.com/robot/send?access_token=cf6d88a4f8002893c0609aa009993e7138ca8112d83aae19e42b5d29147939eb"

  # app_pic_url = "https://www.pgyer.com/app/qrcode/U499"
  # app_download_url = "https://www.pgyer.com/U499"
  app_pic_url = "https://www.pgyer.com/app/qrcode/U499"
  app_download_url = "https://www.pgyer.com/U499"
tipsUser = 
  {
     "msgtype": "markdown",
     "markdown": {
         "title":"微光iOS#{app_Environmental} #{app_version}",
         "text": "iOS 
 #{app_name}#{app_Environmental} #{app_version}
><font color=#DC143C size=6 face=\"黑体\"></font>
#{app_change_content}
\n
>![screenshot](#{app_pic_url})\n> 
## [#{app_Environmental}不能扫二维码请点我](#{app_download_url})"
     },
    "at": {
        "isAtAll": true
    }
 }

  uri = URI.parse(dingTalk_url)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true

  request = Net::HTTP::Post.new(uri.request_uri)
  request.add_field('Content-Type', 'application/json')
  request.body = tipsUser.to_json

  response = https.request(request)
  puts "------------------------------"
  puts "Response #{response.code} #{response.message}: #{response.body}"

  lane :pgyer_debug do |options|
    pgyer(api_key: "9be763cf7766a51db7cd7fa0b571e5dd", 
    user_key: "2d2bf6fb3df3a5c17c66f155c0feb8df")
  end

  end

end
